package com.snatch.snatchmgr.core;


import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.LinkedBlockingQueue;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.snatch.common.utils.SystemUtil;
import com.snatch.common.vo.NodeTaskInput;
import com.snatch.common.vo.RatioDataInput;
import com.snatch.snatchmgr.service.MatchDataService;
import com.snatch.snatchmgr.system.SystemData;
import com.snatch.snatchmgr.vo.NodeSnatch;
import com.snatch.snatchmgr.vo.SnatchItem;


@Component
@Scope ("prototype")
public class SnatchDataThread extends Thread{
	// 日志对象
	private Logger log = Logger.getLogger(SnatchDataThread.class);
	
	private NodeTaskInput nodeTaskInput;
	
	private boolean flag = true;
	
	private SnatchItem snatchItem;
	
	@Autowired
	private MatchDataService matchDataService;
	
	public ConcurrentLinkedQueue<String> dataQueue;
	
	@Override
	public void run() {
		while (flag) {
			try {
				String datastr = null;
				synchronized (dataQueue) {
					while(!dataQueue.isEmpty()){
						datastr = dataQueue.poll();
					}
				}
				if(datastr!=null){
					datastr = SystemUtil.unGZip(datastr);
					RatioDataInput ratioDataInput =  JSON.parseObject(datastr, RatioDataInput.class);
					if(null != ratioDataInput){
						updateSnatchData(ratioDataInput);
						if(ratioDataInput.getSportinfos()!=null && ratioDataInput.getSportinfos().size()!=0){
							SystemData.versionup(nodeTaskInput.getTaskCode());
							matchDataService.doParseXML(ratioDataInput);
						}
						
					}else{
						//错误日志
					}		
				}
				synchronized (dataQueue) {
					if(dataQueue.isEmpty()){
						dataQueue.wait(2000L);
					}
				}
				
				//bobo 做数据判断。
			} catch (Exception e) {
				log.error("出错！",e);
			}
		}
	}
	
	public SnatchItem findSnatchItem(){
		if(null == snatchItem){
			snatchItem  = SystemData.findSnatchItem(nodeTaskInput.getWebCode(), nodeTaskInput.getGameCode(),
					nodeTaskInput.getItemName(),nodeTaskInput.getItemType());
		}
		return snatchItem;
		
	}
	
	public void updateSnatchData(RatioDataInput ratioDataInput){
		long time = System.currentTimeMillis();
		SnatchItem snatchItem =  findSnatchItem();
		snatchItem.setDataChangeTiem(time);
		snatchItem.setVersion(ratioDataInput.getVersion());
		snatchItem.setSumPage(ratioDataInput.getSumPage());
		for(NodeSnatch node : snatchItem.getNodelist()){
			if(node.getNodeOnlyCode().equals(ratioDataInput.getOnlyCode())){
				node.setError(ratioDataInput.getError());
				node.setSnatchCost(ratioDataInput.getSnatchCost());
				node.setVersion(ratioDataInput.getVersion());
			}
		}
	}
	

	public void destroySnatchItemThread(){
		flag = false;
	}




	public NodeTaskInput getNodeTaskInput() {
		return nodeTaskInput;
	}


	public void setNodeTaskInput(NodeTaskInput nodeTaskInput) {
		this.nodeTaskInput = nodeTaskInput;
	}


	public boolean isFlag() {
		return flag;
	}

	public void setFlag(boolean flag) {
		this.flag = flag;
	}

	public ConcurrentLinkedQueue<String> getDataQueue() {
		return dataQueue;
	}

	public void setDataQueue(ConcurrentLinkedQueue<String> dataQueue) {
		this.dataQueue = dataQueue;
	}


	

}
